package top.fyukina.fymil.parsers;

import org.dom4j.Element;
import top.fyukina.fymil.internal.cmds.Command;
import top.fyukina.fymil.elements.Environment;
import top.fyukina.fymil.exceptions.NoSuchCmdException;
import java.util.HashMap;
import java.util.Map;

public class CommandParser implements RegistrableParser<Void, String, Command> {
    private static final Map<String, Command> cmdLib = new HashMap<>();
    @Override
    public Void parse(Element tag, Environment env) {
        String cmdName = tag.getName();
        Command cmd = get(cmdName);
        if (cmd == null) {
            throw new NoSuchCmdException(cmdName);
        }
        try {
            cmd.run(tag, env);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public Map<String, Command> getLib() {
        return cmdLib;
    }
    @Override
    public ParserType registerName() {
        return ParserType.CMD;
    }
}